package words

import (
	"context"

	"star/internal/dao"
	"star/internal/model/entity"
)

type ListInput struct {
	Uid  uint
	Word string
	Page int
	Size int
}

func (w *Words) List(ctx context.Context, in ListInput) (list []entity.Words, total int, err error) {
	// 对于查询初始值的处理
	if in.Page == 0 {
		in.Page = 1
	}
	if in.Size == 0 {
		in.Size = 15
	}

	var (
		cls = dao.Words.Columns()
		orm = dao.Words.Ctx(ctx)
	)
	// 组成查询链
	if in.Uid > 0 {
		orm = orm.Where(cls.Uid, in.Uid)
	}

	// 模糊查询
	if len(in.Word) != 0 {
		orm = orm.WhereLike(cls.Word, "%"+in.Word+"%")
	}
	orm = orm.OrderDesc(cls.CreatedAt).OrderDesc(cls.Id).Page(in.Page, in.Size)
	if err = orm.ScanAndCount(&list, &total, true); err != nil {
		return
	}
	return
}
